package com.cust.yx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cust.yx.acl.mapper.PermissionMapper;
import com.cust.yx.acl.service.AdminService;
import com.cust.yx.acl.service.PermissionService;
import com.cust.yx.acl.service.RolePermissionService;
import com.cust.yx.acl.utils.PermissionHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cust.yx.model.acl.Permission;
import com.cust.yx.model.acl.RolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionService rolePermissionService;


    //查询所有菜单
    @Override
    public List<Permission> queryAllPermission() {
        //1 查询所有菜单
        List<Permission> allPermissionList = baseMapper.selectList(null);

        //2 转换要求数据格式
        List<Permission> result = PermissionHelper.buildPermission(allPermissionList);
        return result;
    }

    //递归删除菜单
    @Override
    public void removeChildById(Long id) {
        //创建list集合idList，封装所有要删除菜单id
        List<Long> idList = new ArrayList<>();

        //根据当前菜单id，获取当前菜单下面所有子菜单，
        //如果子菜单下面还有子菜单，都要获取到
        //递归找当前菜单子菜单
        this.getAllPermissionId(id, idList);

        //设置当前菜单id
        idList.add(id);
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getPermissionId,idList);
        rolePermissionService.remove(wrapper);
        //调用方法根据多个菜单id删除
        baseMapper.deleteBatchIds(idList);
    }

    //为角色进行权限分配
    @Override
    public void saveRolePermission(Long roleId, Long[] permissionIds) {
        //删除之前的数据
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId, roleId);
        rolePermissionService.remove(wrapper);

        //2 重新分配
        List<RolePermission> list = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(permissionId);
            rolePermission.setRoleId(roleId);
            //放到list集合
            list.add(rolePermission);
        }
        //调用方法添加
        rolePermissionService.saveBatch(list);
    }

    //根据角色获取菜单
    @Override
    public List<Permission> selectAllPermissionByRoleId(Long roleId) {
        List<Permission> allPermissionList = baseMapper.selectList(null);
        //根据角色id获取角色权限
        List<RolePermission> rolePermissionList = rolePermissionService.list(new QueryWrapper<RolePermission>().eq("role_id",roleId));
        for (int i = 0; i < allPermissionList.size(); i++) {
            Permission permission = allPermissionList.get(i);
            for (int m = 0; m < rolePermissionList.size(); m++) {
                RolePermission rolePermission = rolePermissionList.get(m);
                if(rolePermission.getPermissionId().equals(permission.getId())) {
                    permission.setSelect(true);
                }
            }
        }
        List<Permission> permissionList = PermissionHelper.buildPermission(allPermissionList);
        return permissionList;
    }

    //递归找当前菜单下面的所有子菜单
    //第一个参数是当前菜单id
    //第二个参数最终封装list集合，包含所有菜单id
    private void getAllPermissionId(Long id, List<Long> idList) {
        //根据当前菜单id查询下面子菜单
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getPid, id);
        List<Permission> childList = baseMapper.selectList(wrapper);
        if(!StringUtils.isEmpty(childList)){
            //递归查询是否还有子菜单，有继续递归查询
            childList.stream().forEach(item -> {
                //封装菜单id到idList集合里面
                idList.add(item.getId());
                //递归
                this.getAllPermissionId(item.getId(), idList);
            });
        }
    }
}
